﻿using System;
using LinqToTwitter;
using Microsoft.SystemCenter.Orchestrator.Integration;
using SocialMediaIntegrationPack.Properties;

namespace SocialMediaIntegrationPack
{
    [Activity("Post Direct Message")]
    public class PostDirectMessageAct : TwitterActivity
    {
        [ActivityInput("Text")]
        public string MsgText { get; set; }

        [ActivityInput("User")]
        public string User { get; set; }

        public PostDirectMessageAct()
        {            
        }

        [ActivityMethod]
        public void Execute()
        {
            try
            {
                if (MsgText.Length > STATUS_MAX_LENGTH)
                {
                    throw new ArgumentException(Resources.StatusMaxLengthError);
                }

                if (MsgText.Length == 0)
                {
                    throw new ArgumentException(Resources.StatusMinLengthError);
                }

                if (string.IsNullOrEmpty(User))
                {
                    throw new ArgumentException(Resources.UserNullOtEmptyException);    
                }

                var twitterContext = Authorize();
                var message = twitterContext.NewDirectMessage(User, MsgText);
                Response = string.Format(Resources.PostDirectMessageResponse, message.RecipientScreenName, message.ID);
            }
            catch (Exception exception)
            {
                LogException(exception);
                throw;
            }            
        }
    }
}
